package com.cn.algorithm01;

import java.util.*;

/**
 * @author hels
 * @version 1.0
 * @date 2021/10/22 9:31
 */
public class Code15_ShowCpmparator {

    private static class Student {
        private String name;
        private int age;
        private int id;

        public Student (String name, int age, int id) {
            this.name = name;
            this.age = age;
            this.id = id;
        }
    }

    // 自定义对象比较器
    public static class CompareById implements Comparator<Student> {
        public int compare(Student o1, Student o2) {
            if (o1.id < o2.id) {
                return -1;
            } else if (o1.id > o2.id) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    // Integer比较器
    public static class MyComparator implements Comparator<Integer> {

        public int compare(Integer o1, Integer o2) {
            if (o1 < o2) {
                return 1;
            } else if (o1 > 02) {
                return -1;
            } else {
                return 0;
            }
        }
    }

    // 打印数组
    public static void printArray(int[] arr) {
        for (int i : arr) {
            System.out.print(i);
        }
        System.out.println();
    }

    // 打印队形
    private static void printStudent(Student[] arr) {
        for (Student s : arr) {
            System.out.println(s.name + " " + s.age + " " + s.id);
        }
    }


    public static void main(String[] args) {
        Student s1 = new Student("张3", 22, 3);
        Student s2 = new Student("张1", 21, 7);
        Student s3 = new Student("张5", 12, 5);
        Student s4 = new Student("张2", 33, 2);

        // Student进行堆排序
        PriorityQueue<Student> heap2 = new PriorityQueue<Student>(new CompareById());
        heap2.add(s1);
        heap2.add(s2);
        heap2.add(s3);
        heap2.add(s4);
        System.out.println(heap2.peek().id);
        while (!heap2.isEmpty()) {
            Student s = heap2.poll();
            System.out.println(s.name + " " + s.age + " " + s.id);
        }


        // Integer的堆排序
        /*PriorityQueue<Integer> heap = new PriorityQueue<Integer>(new MyComparator());
        heap.add(2);
        heap.add(3);
        heap.add(5);
        heap.add(1);
        System.out.println(heap.peek());
        while (!heap.isEmpty()) {
            System.out.print(heap.poll() + " ");
        }
        System.out.println();*/



        //自定义对象数组排序
        /* int[] arr = {3,4,6,3,2,6,8,2,4};
        printArray(arr);
        Arrays.sort(arr);
        printArray(arr);*/


        /*
        Student s1 = new Student("张3", 22, 3);
        Student s2 = new Student("张1", 21, 1);
        Student s3 = new Student("张5", 12, 5);
        Student s4 = new Student("张2", 33, 2);

        Student[] students = {s1, s2, s3, s4};
        printStudent(students);
        System.out.println("===");
        Arrays.sort(students, new CompareById());
        printStudent(students);*/

        /*ArrayList<Student> arrayList = new ArrayList<Student>();
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        for (Student s : arrayList) {
            System.out.println(s.name + " " + s.age + " " + s.id);
        }
        System.out.println("===");
        arrayList.sort(new CompareById());
        for (Student s : arrayList) {
            System.out.println(s.name + " " + s.age + " " + s.id);
        }*/




    }


}
